package com.zchat.api.modules.system;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;

import android.os.Environment;
import android.os.StatFs;

import com.zchat.api.FileDirProvider;
import com.zchat.api.modules.Module;

/**
 * @author Oliverzhu
 *
 */
public class SystemModule extends Module {
	
	/**
	 * 检查sd卡是否挂载
	 * @return
	 */
	public boolean isSDCardMounted() {
		String state = Environment.getExternalStorageState();
		if(!Environment.MEDIA_MOUNTED.equals(state)) {
			return false;
		}
		return true;
	}
	
	/**
	 * 检查sd卡所剩控件
	 * @return
	 */
	public long freeSize() {
		File file = Environment.getExternalStorageDirectory();
		StatFs sf = new StatFs(file.getPath());
		return ((long)sf.getAvailableBlocks())*((long)sf.getBlockSize());
	}
	
	
	/**
	 * 创建应用文件夹
	 * @return
	 * @throws IOException 
	 */
	public boolean createFS() throws Exception {
		File file = Environment.getExternalStorageDirectory();
		if(!file.canWrite()) {
			return false;
		}
		ArrayList<File> dirs = new ArrayList<File>();
		Field[] fields = FileDirProvider.class.getDeclaredFields();
		for(Field f : fields) {
			dirs.add(new File(f.get(null).toString()));
		}
//		File root = new File(file.getPath() + FileDirProvider.ROOT);
//		dirs.add(root);
//		File log = new File(file.getPath() + FileDirProvider.LOG);
//		dirs.add(log);
//		File cache = new File(file.getPath() + FileDirProvider.CACHE);
//		dirs.add(cache);
//		File download = new File(file.getPath() + FileDirProvider.DOWNLOAD);
//		dirs.add(download);
//		File etc = new File(file.getPath() + FileDirProvider.ETC);
//		dirs.add(etc);
//		File upgrade = new File(file.getPath() + FileDirProvider.UPGRADE);
//		dirs.add(upgrade);
		for(File dir : dirs) {
			if(!dir.exists()) {
				dir.mkdirs();
			}
		}
		return true;
	}
	
	@Override
	public String toString() {
		return "SystemModule";
	}
}
